<?php
class User extends AppModel {

	var $validate = array(

		'firstname'   => array(
			'alphanumeric' => array(
				'rule' => 'alphanumeric',
				'message' => 'Username may only consist of letter and numbers'
			),
			'notEmpty' => array(
				'rule' => VALID_NOT_EMPTY,
				'message' => 'Please enter your firstname'
			)
		),
		'lastname'   => array(
			'letters' => array(
				'rule' => 'alphanumeric',
				'message' => 'Username may only consist of letter and numbers'
			),
			'notEmpty' => array(
				'rule' => VALID_NOT_EMPTY,
				'message' => 'Please enter your lastname'
			)
		),
		'email' => array(
			'pattern' => array(
				'rule' => array('email', false), // set to true if online
				'message' => 'Invalid email address format or unknown domain'
			),

			'isUnique' => array(
				'rule' => 'checkUnique',
				'message' => 'Diese email existiert bereits. Falls Sie Ihr Passwort vergessen haben, klicken Sie bitte <a href="/">hier</a>.' // refactor!!
			),

			'notEmpty' => array(
				'rule' => VALID_NOT_EMPTY,
				'message' => 'Please enter your email'
			)
		),
		'password'   => array(
			'notEmpty' => array(
				'rule' => VALID_NOT_EMPTY,
				'message' => 'Please enter your password'
			)
		),
		'confirm_password'   => array(
			'alphanumeric' => array(
				'rule' => 'alphanumeric',
				'message' => 'Password may only consist of letter and numbers'
			),
			'length' => array(
				'rule' => array('between', 6, 20),
				'message' => 'Password must be between 6 and 20 characters in length'
			),
			'match' => array(
				'rule' => 'passwordsMatch',
				'message' => 'Your passwords do not match'
			),
			'notEmpty' => array(
				'rule' => VALID_NOT_EMPTY,
				'message' => 'Please retype your password'
			)
		)
	);

	function checkUnique($data) {

		return $this->isUnique(array(
				'email' => $this->data['User']['email'])
			);
	}

	function passwordsMatch() {

		if (!$this->exists()) {
			$hashedConfirm = Security::hash($this->data['User']['confirm_password']);
			if ($hashedConfirm != $this->data['User']['password']) {
				return false;
			}
		}
		return true;
	}

}




?>

